﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace BreadWorkerRole
{
    public class WorkerRole : RoleEntryPoint
    {
        private int msInAnHour = 60 * 60 * 1000;
        private class VerseInfo
        {
            public string BreadName { get; set; }
            public string VerseName { get; set; }
        }

        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("BreadWorkerRole entry point called", "Information");

            while (true)
            {
                // sleep 1 hour 
                Thread.Sleep(msInAnHour);

                if (StartOfDay())
                {
                    NotifyMobileDevice();
                }

                Trace.WriteLine("Working", "Information");
            }
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            return base.OnStart();
        }

        private bool StartOfDay()
        {
            int hour = System.DateTime.UtcNow.Hour;
            if (hour == 0)
            {
                return true;
            }
            return false;
        }

        private void NotifyMobileDevice()
        {
            VerseInfo vi = GetVerseInfo();
            int commentCount = GetCommentCount();
        }

        private VerseInfo GetVerseInfo()
        {
            VerseInfo vi = new VerseInfo();

            //TODO: outbound http to get the info
            return vi;
        }

        private int GetCommentCount()
        {
            int count = 0;

            //TODO: get comment count from azure storage
            return count;
        }

    }
}
